package cn.icebreak.cartoon.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.icebreak.cartoon.R;
import cn.icebreak.cartoon.global.Route;
import cn.icebreak.cartoon.ui.adapter.ContentPageAdapter;
import cn.icebreak.cartoon.ui.customview.CustomViewPager;
import cn.icebreak.cartoon.ui.fragment.CollectionFragment;
import cn.icebreak.cartoon.ui.fragment.DummyFragment;
import cn.icebreak.cartoon.ui.fragment.DynamicFragment;
import cn.icebreak.cartoon.ui.fragment.HomePageFragment;
import cn.icebreak.cartoon.ui.fragment.MineFragment;
import cn.icebreak.cartoon.ui.read.ItemsFragment;

/**
 * Created by Codeant on 2018/7/9.
 */

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private static final int PAGE_ID_RECOMMEND    = 0;
    private static final int PAGE_ID_CATEGORY  = 1;
    private static final int PAGE_ID_COLLECTION = 2;
    private static final int PAGE_ID_MINE  = 3;
    private CustomViewPager contentVp;
    private TextView recommendTv;
    private TextView categoryTv;
    private TextView collectionTv;
    private TextView mineTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findAllViews();
        initViews();
    }

    private void findAllViews(){
        contentVp = (CustomViewPager) findViewById(R.id.vp_main_content);
        recommendTv = (TextView) findViewById(R.id.tv_main_bar_recommend);
        categoryTv = (TextView) findViewById(R.id.tv_main_bar_category);
        collectionTv = (TextView) findViewById(R.id.tv_main_bar_collection);
        mineTv = (TextView) findViewById(R.id.tv_main_bar_mine);
    }

    private void initViews(){

        List<Fragment> pages = new ArrayList<>();
        pages.add(new DummyFragment()); // HomePageFragment
        pages.add(new DynamicFragment());
        pages.add(new DummyFragment()); // CollectionFragment
        pages.add(new DummyFragment()); // MineFragment
        ContentPageAdapter adapter = new ContentPageAdapter(getSupportFragmentManager(),pages);
        contentVp.setAdapter(adapter);
        // 禁止滑动(涉及到高低版本适配时的状态栏背景问题)
        contentVp.setScrollEnable(false);

        choicePage(PAGE_ID_CATEGORY);

        recommendTv.setOnClickListener(this);
        categoryTv.setOnClickListener(this);
        collectionTv.setOnClickListener(this);
        mineTv.setOnClickListener(this);
    }


    private void choicePage(int pageId){
        // 清除状态
        recommendTv.setTextColor(getResources().getColor(R.color.bar_text_normal));
        categoryTv.setTextColor(getResources().getColor(R.color.bar_text_normal));
        collectionTv.setTextColor(getResources().getColor(R.color.bar_text_normal));
        mineTv.setTextColor(getResources().getColor(R.color.bar_text_normal));
        recommendTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_recommend,0,0);
        categoryTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_category,0,0);
        collectionTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_collection,0,0);
        mineTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_mine,0,0);
        // 设置新的item状态
        switch (pageId){
            case PAGE_ID_RECOMMEND:
                recommendTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_recommend_select,0,0);
                recommendTv.setTextColor(getResources().getColor(R.color.bar_text_select));
                contentVp.setCurrentItem(0);
                break;
            case PAGE_ID_CATEGORY:
                categoryTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_category_select,0,0);
                categoryTv.setTextColor(getResources().getColor(R.color.bar_text_select));
                contentVp.setCurrentItem(1);
                break;
            case PAGE_ID_COLLECTION:
                collectionTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_collection_select,0,0);
                collectionTv.setTextColor(getResources().getColor(R.color.bar_text_select));
                contentVp.setCurrentItem(2);
                break;
            case PAGE_ID_MINE:
                mineTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_mine_select,0,0);
                mineTv.setTextColor(getResources().getColor(R.color.bar_text_select));
                contentVp.setCurrentItem(3);
                break;
            default:
                recommendTv.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_tab_recommend_select,0,0);
                recommendTv.setTextColor(getResources().getColor(R.color.bar_text_select));
                contentVp.setCurrentItem(0);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_main_bar_recommend:
                choicePage(PAGE_ID_RECOMMEND);
                break;
            case R.id.tv_main_bar_category:
                choicePage(PAGE_ID_CATEGORY);
                break;
            case R.id.tv_main_bar_collection:
                choicePage(PAGE_ID_COLLECTION);
                break;
            case R.id.tv_main_bar_mine:
                choicePage(PAGE_ID_MINE);
                break;
            default:
                choicePage(PAGE_ID_RECOMMEND);
                break;
        }
    }
}
